package insight.Builder;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author huu
 * @create 2025-08-26-15:03
 */
public class SQL {
    private SQL(){
    }
    public static SqlBuilder builder(SqlType sqlType){
        return new SqlBuilder(sqlType);
    }

    public static SelectBuilder select(String... columns){
        return new SelectBuilder(columns);
    }

    public static UpdateBuilder update(String table){
        return new UpdateBuilder(table);
    }

    public static InsertBulider insert(){
        return new InsertBulider();
    }

    public static DeleteBuilder delete(){
        return new DeleteBuilder();
    }

/*
* select * from user where id = 1;
* update user set name = 'TTDB' where id = 1;
* insert into user (name, age) values ('TTDB', 18);
* delete from user where id = 1;
* */


    interface tableStage{
        whereStage table(String table);
    }
    interface whereStage{
        whereStage where(String where);
        String build();
    }

    public interface setStage {
        whereStage set(String column, String value);
    }

//    public static class SelectBuilder implements tableStage, whereStage{
//        private String table;
//        private String where;
//        private String[] columns;
//        private SelectBuilder(String[] columns){
//            this.columns = columns;
//        }
//        @Override
//        public whereStage table(String table){
//            this.table = table;
//            return this;
//        }
//        @Override
//        public whereStage where(String where){
//            this.where = where;
//            return this;
//        }
//        @Override
//        public String build(){
//            StringBuilder sql = new StringBuilder();
//            sql.append("SELECT ");
//            // 修复：检查columns是否为null
//            if (columns != null && columns.length > 0) {
//                sql.append(String.join(", ", columns));
//            } else {
//                sql.append("*");
//            }
//            sql.append(" FROM ");
//            sql.append(table);
//            // 修复：检查where条件是否存在
//            if (where != null && !where.isEmpty()) {
//                sql.append(" WHERE ");
//                sql.append(where);
//            }
//            return sql.toString();
//        }
//
//    }


//    public static UpdateBuilder update(){
//        return new UpdateBuilder();
//    }


    public static class SqlBuilder{
        final SqlType sqlType;
        private SqlBuilder(SqlType sqlType){
            this.sqlType = sqlType;
        }
        String[] columns;
        String table;
        String where;
        Map<String, String> setMap = new LinkedHashMap<>();
        public SqlBuilder columns(String... columns){//可变参数
            this.columns = columns;
            return this;
        }
        public SqlBuilder table(String table){
            this.table = table;
            return this;
        }
        public SqlBuilder where(String where){
            this.where = where;
            return this;
        }
        public SqlBuilder set(String column, String value){
            setMap.put(column, value);
            return this;
        }

        public String build(){
            StringBuilder sql = new StringBuilder();
            switch (sqlType){
                case SELECT ->{
                    sql.append("SELECT ");
                    // 修复：检查columns是否为null
                    if (columns != null && columns.length > 0) {
                        sql.append(String.join(", ", columns));
                    } else {
                        sql.append("*");
                    }
                    sql.append(" FROM ");
                    sql.append(table);
                    // 修复：检查where条件是否存在
                    if (where != null && !where.isEmpty()) {
                        sql.append(" WHERE ");
                        sql.append(where);
                    }
                }
                case UPDATE -> {
                    sql.append("UPDATE ");
                    sql.append(table);
                    sql.append(" SET ");
                    //流又来了
                    String collect = setMap.entrySet()
                            .stream()
                            .map(entry -> entry.getKey() + " = " + entry.getValue())
                            .collect(Collectors.joining(","));
                    sql.append(collect);
                }
                default -> throw new RuntimeException("暂不支持");
            }
            return sql.toString();
        }
    }

    enum SqlType {
        SELECT, INSERT, UPDATE, DELETE
    }


//    public static class UpdateBuilder {
//        private String tableName;
//        private Map<String, Object> updateColumns = new LinkedHashMap<>();
//        private String where;
//        private UpdateBuilder(String tableName){
//            this.tableName = tableName;
//        }
//        public UpdateBuilder set(String column, String value){
//            updateColumns.put(column, value);
//            return this;
//        }
//        public UpdateBuilder where(String where){
//            this.where = where;
//            return this;
//        }
//        public String build(){
//            StringBuilder sql = new StringBuilder();
//                    sql.append("UPDATE ");
//                    sql.append(tableName);
//                    sql.append(" SET ");
//                    //流又来了
//                    String collect = updateColumns.entrySet()
//                            .stream()
//                            .map(entry -> entry.getKey() + " = " + entry.getValue())
//                            .collect(Collectors.joining(","));
//                    sql.append(collect);
//                    sql.append(" WHERE ").append(where);
//            return sql.toString();
//        }
//    }

}
